<?

//Data loading
$title='CREDIT NOTE';
$title2='';
if (GetOfficeID()) $perush = GetOfficeLocation (GetOfficeID());
else $perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';

$DEFINE_FONT=0;
$pdf=new PDFReport("P","mm","Letter");
$pdf->AddPage();
$Result = GridList ();
$pdf->GridTable($Result);
$pdf->Output();

function GridList () {
	  $Field = ReadHeader(GetParam("nobkt",""));
	  global $DecimalPoint;
	  $DecimalPoint = ($Field->curr<>"IDR")?2:0;
//	  print $DecimalPoint;die();
	  $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
      $SQL = "SELECT * FROM ais_memorial WHERE nobkt='".GetParam("nobkt","")."' AND urut<>999 ORDER BY urut";
//	  die($SQL);
      return $DBConnection->dbc->get_results($SQL,ARRAY_A);
}

function ReadHeader($nokwit) {
	global $DBConnection;

    $SQL = "
      SELECT ais_memorial.*,DATE_FORMAT(depdat,'%d %M, %Y') as spell_date,ais_memorial.noref, DATE_FORMAT(tglbkt,'%d %M, %Y') as trans_date, SUM(jumlah)  as TOTALPAID, CUADDR, CUPHONE, CUCODE, DATE_FORMAT(duedate,'%d/%m/%Y') as trans_duedate
      FROM ais_memorial
      LEFT JOIN tbldebtur ON ais_memorial.debtur=tbldebtur.cucode
      WHERE nobkt='$nokwit' AND urut=999 GROUP BY nobkt 
    ";
    //print $SQL;die();
  return $DBConnection->dbc->get_row($SQL);
}
class PDFReport extends PDF {
	function Header() {
		global $title,$title2,$perush,$page,$DEFINE_FONT,$DBConnection;

		//$this->Ln();$this->Ln();$this->Ln();$this->Ln();$this->Ln();
		if ($DEFINE_FONT==0) {
			$this->AddFont('Arial','','arialn.php');
			$DEFINE_FONT=1;
		}
        	$Field = ReadHeader(GetParam("nobkt",""));
                $this->spell_date = strtoupper($Field->spell_date);
                $this->trans_date = strtoupper($Field->trans_date);
                list($this->year,$this->month,$this->day) = explode("-",$Field->tglbkt);


               $this->Image("logomtcon.JPG",10,10,80,15);
				for ($x=1;$x<=2;$x++)
				{$this->Cell(110,5,"","",0);
				$this->Ln();
				}
                $this->Cell(200,20,"",0,1,'L',0);
                $this->Cell(50,7,"",0,0,'L',0);
                $this->SetFont('Arial','',16);
                $this->Cell(100,7,$title,0,0,'C',0);
                $this->SetFont('Arial','',10);
                $this->Cell(200,3,"",0,1,'L',0);		        
                $this->Ln();$this->Ln();$this->Ln();$this->Ln();
                $this->Ln();
                //Header Debit Note
                //baris 1
                //$this->SetXY(5,65);
				$this->Cell(10,5," ","",0,'L',0);
				$this->Cell(110,5," ".$Field->debnam,"LT",0,'L',0);
				$this->Cell(2,5,"","LR",0,'C',0);
				$this->Cell(20,5," NUMBER","TB",0,'L',0);
				$this->Cell(2,5," :","TB",0,'L',0);
				$this->Cell(50,5,GetParam("nobkt",""),"RTB",0,'L',0);
				$this->Ln();

				$this->Cell(10,5," ","",0,'L',0);
				$this->Cell(110,5,trim($Field->CUADDR),"L",0,'L',0);
				//$this->Cell(110,5," ","L",0,'L',0);
				$this->Cell(2,5,"","LR",0,'C',0);
				$this->Cell(20,5," REFERENCE","B",0,'L',0);
				$this->Cell(2,5," :","B",0,'L',0);
				$this->Cell(50,5,trim($Field->noref),"RB",0,'L',0);
				$this->Ln();

				$this->Cell(10,5," ","",0,'L',0);
				$this->Cell(80,5," ","L",0,'L',0);
				$this->Cell(30,5," CODE : ".$Field->debtur,"",0,'L',0);
				$this->Cell(2,5,"","LR",0,'C',0);
				$this->Cell(20,5," CURRENCY","B",0,'L',0);
				$this->Cell(2,5," :","B",0,'L',0);
				$this->Cell(50,5,$Field->curr,"RB",0,'L',0);
				$this->Ln();

				$this->Cell(10,5," ","",0,'L',0);
				$this->Cell(110,5,"We Credit Your Account",1,0,'C',0);
				$this->Cell(2,5,"","LR",0,'C',0);
				$this->Cell(20,5," DUEDATE","B",0,'L',0);
				$this->Cell(2,5," :","B",0,'L',0);
				$this->Cell(50,5,$Field->trans_duedate,"RB",0,'L',0);
		        $this->Ln();
		        $this->Ln();

				$this->Cell(10,8," ","",0,'L',0);
				$this->Cell(10,8,"NO.",1,0,'C',0);
				$this->Cell(90,8,"DESCRIPTION",1,0,'C',0);
				$this->Cell(30,8,"AMOUNT",1,0,'C',0);
				$this->Cell(20,8,"CODE",1,0,'C',0);
				$this->Cell(24,8,"OFFICE",1,0,'C',0);
				$this->Cell(10,8,"UU",1,0,'C',0);
				$this->Ln();
	}

	function GridTable($MyResult) {
	    //Column widths
	global $DBConnection,$DBName,$DecimalPoint;
	$DBC1 = new mydb(DB_USER, DB_PASSWORD, OfficeDB1, DB_HOST);
	//$w=array(5,10,80,30,40,20);
	$w=array(10,90,30,20,24,10);
	    //Data
        if ($MyResult)
	    foreach($MyResult as $row)
	    {
            $Total = $Total + $row["jumlah"];
			$brs++;
			$this->Cell(10,5," ","",0,'L',0);
	        $this->Cell($w[0],5,$row["urut"].".","LR",0,'R',0);
	        $this->Cell($w[1],5,$row["uraian"],"",0,'L',0);
            $this->Cell($w[2],5,FormatNumber($row["jumlah"],$DecimalPoint),"R",0,'R',0);
            $this->Cell($w[3],5,$row["skada"],"",0,'C',0);
	        $this->Cell($w[4],5,"",0,0,'R');
            $this->Cell($w[5],5,$row["kdact"],"R",0,'C',0);
	        $this->Ln();
	        if (strlen($row["uraian_a"])) {
				$brs++;
				$this->Cell(10,5," ","",0,'L',0);
				$this->Cell($w[0],5,"","LR",0,'R',0);
				$this->Cell($w[1],5,"            ".$row["uraian_a"],"",0,'L',0);
				$this->Cell($w[2],5,"","R",0,'R',0);
				$this->Cell($w[3],5,"","",0,'C',0);
				$this->Cell($w[4],5,"","",0,'C',0);
				$this->Cell($w[5],5,"","R",0,'R',0);
				$this->Ln();
			}

	        if (strlen($row["uraian_b"])) {
				$brs++;
				$this->Cell(10,5," ","",0,'L',0);
				$this->Cell($w[0],5,"","LR",0,'R',0);
				$this->Cell($w[1],5,"            ".$row["uraian_b"],"",0,'L',0);
				$this->Cell($w[2],5,"","R",0,'R',0);
				$this->Cell($w[3],5,"","",0,'C',0);
				$this->Cell($w[4],5,"","",0,'C',0);
				$this->Cell($w[5],5,"","R",0,'R',0);
				$this->Ln();
			}

	        if (strlen($row["uraian_c"])) {
				$brs++;
				$this->Cell(10,5," ","",0,'L',0);
				$this->Cell($w[0],5,"","LR",0,'R',0);
				$this->Cell($w[1],5,"            ".$row["uraian_c"],"",0,'L',0);
				$this->Cell($w[2],5,"","R",0,'R',0);
				$this->Cell($w[3],5,"","",0,'C',0);
				$this->Cell($w[4],5,"","",0,'C',0);
				$this->Cell($w[5],5,"","R",0,'R',0);
				$this->Ln();
			}

	        if (strlen($row["uraian_d"])) {
				$brs++;
				$this->Cell(10,5," ","",0,'L',0);
				$this->Cell($w[0],5,"","LR",0,'R',0);
				$this->Cell($w[1],5,"            ".$row["uraian_d"],"",0,'L',0);
				$this->Cell($w[2],5,"","R",0,'R',0);
				$this->Cell($w[3],5,"","",0,'C',0);
				$this->Cell($w[4],5,"","",0,'C',0);
				$this->Cell($w[5],5,"","R",0,'R',0);
				$this->Ln();
			}
	    }

        $SQL = "SELECT * FROM ais_memorial WHERE nobkt='".GetParam("nobkt","")."' AND urut=999 ";
        $Field = $DBC1->dbc->get_row($SQL);
        $brs++;
		$this->Cell(10,5," ","",0,'L',0);
		$this->Cell($w[0],5,"","LR",0,'R',0);
	    $this->Cell($w[1],5,"","",0,'L',0);
	    $this->Cell($w[2],5,"","R",0,'R',0);
	    $this->Cell($w[3],5,"","",0,'C',0);
	    $this->Cell($w[4],5,"","",0,'C',0);
	    $this->Cell($w[5],5,"","R",0,'R',0);
	    $this->Ln();
	    $brs++;
		$this->Cell(10,5," ","",0,'L',0);
	    $this->Cell($w[0],5,"999.","LR",0,'R',0);
	    //$this->Cell($w[1],8,"",0,0,'R');
	    $this->Cell($w[1],5,"TOTAL: ","",0,'R',0);
        if ($Field->curr=="IDR") $Total = round($Total);	    
        $this->Cell($w[2],5,FormatNumber($Total,$DecimalPoint),"R",0,'R',0);
        $this->Cell($w[3],5,$Field->skada,"",0,'C',0);
		$this->Cell($w[4],5,"","",0,'C',0);
        $this->Cell($w[5],5,$Field->kdact,"R",0,'R',0);        
   	    $this->Ln();
		$brs = $brs + 2;
		           
	    for ($x=1;$x<=2;$x++){
		$this->Cell(10,5," ","",0,'L',0);
	    $this->Cell($w[0],5,"","LR",0,'R',0);
	    $this->Cell($w[1],5,"","",0,'L',0);
	    $this->Cell($w[2],5,"","R",0,'R',0);
	    $this->Cell($w[3],5,"","",0,'C',0);
	    $this->Cell($w[4],5,"","",0,'C',0);
	    $this->Cell($w[5],5,"","R",0,'R',0);
	    $this->Ln();
            }
            
        $cc = new intToRupiah;
        $GetCurrency = $DBC1->dbc->get_var("
        SELECT DISTINCT tunam
        FROM tblcurrency, ais_memorial
        WHERE ais_memorial.nobkt='".GetParam("nobkt","")."' AND
              tblcurrency.tusymbol=ais_memorial.curr AND
              ktrasl='".GetOfficeID()."'
        ");


	    list($number,$decimal) = explode(".",$Total);

	    $cc = new numtoword;
	    $SpellNumber = strtoupper(trim($cc->en($Total)));
        $brs++;
		$this->Cell(10,5," ","",0,'L',0);
		$this->Cell($w[0],5,"","LR",0,'R',0);
	    $this->Cell($w[1]+$w[2],5,"SAY : ".$SpellNumber." ".$Field->curr,"",0,'L',0);
		$this->Cell($w[3],5,"","L",0,'C',0);
	    $this->Cell($w[4],5,"","",0,'C',0);
	    $this->Cell($w[5],5,"","R",0,'R',0);
        $this->Ln();
        $brs = $brs + 3;
		for ($x=1;$x<=3;$x++){
        $this->Cell(10,5," ","",0,'L',0);
	    $this->Cell($w[0],5,"","LR",0,'R',0);
	    $this->Cell($w[1],5,"","",0,'L',0);
	    $this->Cell($w[2],5,"","R",0,'R',0);
	    $this->Cell($w[3],5,"","",0,'C',0);
	    $this->Cell($w[4],5,"","",0,'C',0);
	    $this->Cell($w[5],5,"","R",0,'R',0);
	    $this->Ln();
        }

        $w=array(10,90,30,20,24,10);
        $brs++;
		$this->Cell(10,5," ","",0,'L',0);
		$this->Cell($w[0],5,"","LR",0,'R',0);
		if (strlen($this->spell_date))
        	$this->Cell($w[1]+$w[2],5,"    ".GetBranchName().", ".$this->spell_date,"LR",0,'L',0);	    
           else
        	$this->Cell($w[1]+$w[2],5,"    ".GetBranchName().", ".$this->trans_date,"LR",0,'L',0);	
	    $this->Cell($w[3],5,"","L",0,'C',0);
        $this->Cell($w[4],5,"","",0,'C',0);
	    $this->Cell($w[5],5,"","R",0,'R',0);
        $this->Ln();

        $brs=$brs+3;
		for ($x=1;$x<=3;$x++){
		$this->Cell(10,5," ","",0,'L',0);
	    $this->Cell($w[0],5,"","LR",0,'R',0);
	    $this->Cell($w[1],5,"","",0,'L',0);
	    $this->Cell($w[2],5,"","R",0,'R',0);
	    $this->Cell($w[3],5,"","",0,'C',0);
	    $this->Cell($w[4],5,"","",0,'C',0);
	    $this->Cell($w[5],5,"","R",0,'R',0);
	    $this->Ln();
            }
            
        $SQL = "SELECT * FROM $DBName.tblprofile WHERE ktrasl='".GetOfficeID()."'";
        $Result = $DBConnection->dbc->get_row($SQL);
		$brs++;
		$this->Cell(10,5," ","",0,'L',0);
        $this->Cell($w[0],5,"","LR",0,'R',0);
        $this->Cell($w[1]+$w[2],5,"     (".$Result->ttdcn.")","LR",0,'L',0);	    //treasury
	    $this->Cell($w[3],5,"","",0,'C',0);
	    $this->Cell($w[4],5,"","",0,'C',0);
	    $this->Cell($w[5],5,"","R",0,'R',0);
         $this->Ln();
		 
         for ($x=1;$x<=25-$brs;$x++){
		$this->Cell(10,5," ","",0,'L',0);
	    $this->Cell($w[0],5,"","LR",0,'R',0);
	    $this->Cell($w[1],5,"","",0,'L',0);
	    $this->Cell($w[2],5,"","R",0,'R',0);
	    $this->Cell($w[3],5,"","",0,'C',0);
	    $this->Cell($w[4],5,"","",0,'C',0);
	    $this->Cell($w[5],5,"","R",0,'R',0);
	    $this->Ln();
            }
		$this->Cell(10,5," ","",0,'L',0);
	    $this->Cell($w[0],5,"","LBR",0,'R',0);
	    $this->Cell($w[1],5,"","B",0,'L',0);
	    $this->Cell($w[2],5,"","RB",0,'R',0);
	    $this->Cell($w[3],5,"","B",0,'C',0);
	    $this->Cell($w[4],5,"","B",0,'C',0);
	    $this->Cell($w[5],5,"","RB",0,'R',0);
	    $this->Ln();$this->Ln();$this->Ln();
		$this->Cell(10,5," ","",0,'L',0);
	    $this->MultiCell(100,5,$Result->bankdn);
	}

	function Footer() {
	    //Closure line
	    //$this->Cell(205,0,'','T');
	}

}


?>
